package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.page.PageQuery;
import com.ruoyi.common.core.page.PageUtil;
import com.ruoyi.common.core.page.ResultEntity;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TProd;
import com.ruoyi.system.domain.TProdRecord;
import com.ruoyi.system.domain.TProdRecordCheck;
import com.ruoyi.system.domain.dto.TProdRecordDTO;
import com.ruoyi.system.domain.vo.TProdRecordCheckVO;
import com.ruoyi.system.domain.vo.TProdRecordVO;
import com.ruoyi.system.service.ITProdRecordCheckService;
import com.ruoyi.system.service.ITProdRecordService;
import com.ruoyi.system.service.ITProdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @Description TODO
 * @Version 1.0.0
 * @Date 2024/10/28 18:20
 * @Author wux
 */
@Api(tags = "产品检验")
@RestController
@RequestMapping("/client/prod/record/check")
public class TProdRecordCheckController {
    @Autowired
    private ITProdRecordCheckService prodRecordCheckService;
    @Autowired
    private ITProdRecordService prodRecordService;
    @Autowired
    private ITProdService prodService;

    @PostMapping("/add")
    @ApiOperation(value = "新增产品检验", response = TProdRecordCheck.class)
    public ResultEntity add(@Valid @RequestBody TProdRecordCheck params) {
        TProdRecord prodRecord = prodRecordService.getOne(new LambdaQueryWrapper<TProdRecord>()
                .eq(TProdRecord::getBatchCode,params.getBatchCode())
                .last("limit 1"));
        TProd prod = prodService.getOne(new LambdaQueryWrapper<TProd>()
                .eq(TProd::getId,prodRecord.getProdId()));
        if(null != prod.getArcLength()){
            params.setRealArcLength(randomInt(prod.getArcLength().intValue()));
        }
        if(null != prod.getRadius()){
            params.setRealRadius(randomInt(prod.getRadius().intValue()));
        }
        if(null != prod.getHeight()){
            params.setRealHeight(randomInt(prod.getHeight().intValue()));
        }
        if(null != prod.getProdLong()){
            params.setRealProdLong(randomInt(prod.getProdLong().intValue()));
        }
        if(null != prod.getProdWide()){
            params.setRealProdWide(randomInt(prod.getProdWide().intValue()));
        }
        if(null != prod.getProThick()){
            params.setRealProThick(randomInt(prod.getProThick().intValue()));
        }
        params.setProdId(prod.getId());
        params.setProdType(prod.getProdType());
        params.setCreateBy(SecurityUtils.getLoginUser().getUser().getId());
        prodRecordCheckService.save(params);
        return ResultEntity.createSuccess();
    }

    @GetMapping("/list")
    @ApiOperation(value = "产品检验记录查询",response = TProdRecordVO.class)
    public ResultEntity listByPage(TProdRecordCheck params, PageQuery page){
        PageUtil.startPage(page);
        List<TProdRecordCheck> list = prodRecordCheckService.list(new LambdaQueryWrapper<TProdRecordCheck>()
                .eq(TProdRecordCheck::getProdType,params.getProdType())
                .orderByDesc(TProdRecordCheck::getCheckTime));
        List<TProdRecordCheckVO> result = new ArrayList<>();
        list.forEach(item -> {
            TProdRecordCheckVO vo = new TProdRecordCheckVO();
            BeanUtils.copyProperties(item,vo);
            TProd prod = prodService.getById(item.getProdId());
            vo.setArcLength(prod.getArcLength());
            vo.setRadius(prod.getRadius());
            vo.setHeight(prod.getHeight());
            vo.setProdLong(prod.getProdLong());
            vo.setProdWide(prod.getProdWide());
            vo.setProThick(prod.getProThick());
            vo.setRebarSpecifications(prod.getRebarSpecifications());
            vo.setSteelPlateSpecifications(prod.getSteelPlateSpecifications());
            vo.setConduitSpecifications(prod.getConduitSpecifications());
            vo.setSteel(prod.getSteel());
            result.add(vo);
        });
        return ResultEntity.createSuccess(result);
    }

    @PostMapping("/delete/{id}")
    @ApiOperation(value = "删除产品检验")
    public ResultEntity delete(@PathVariable("id") Long id){
        return ResultEntity.createSuccess(prodRecordCheckService.removeById(id));
    }


    /**
     * 生成上下5%内随机数
     * @param num
     * @return
     */
    private int randomInt(int num){
        Random random = new Random();
        // 设置随机数范围的开始值和结束值
        int start = num-num/100*5; // 起始数字
        int end = num+num/100*5;   // 结束数字
        // 生成随机数
        int randomNumber = random.nextInt(end - start + 1) + start;
        return randomNumber;
    }

}
